import { errorReport, getUserId } from '@/utils';
import Apis from '@king-fisher/apis';
import { Request } from '@/types/api.d';

declare interface ResponseSuccess {
  data: Response;
  status?: number;
  code?: number;
  error?: number;
}

declare interface ResponseFailed {
  code: number;
  message: string;
}

export default function http({
  url,
  method = 'GET',
  data,
  headers,
  dataType = 'json',
}: Request): Promise<any> {
  return new Promise((resolve, reject) => {
    Apis.request({
      url,
      method: method || 'GET',
      withCredentials: true,
      data,
      headers: {
        ...headers,
      },
      dataType,
      success: (response: ResponseSuccess) => {
        resolve(response.data);
      },
      fail: (err: ResponseFailed) => {
        errorReport({
          name: '接口请求错误',
          message: `出错接口的url:${url},用户的userid:${getUserId()}`,
          stock: err?.stock,
        });
        reject(err);
      },
      complete: (res: object) => {
        // nothing to do
      },
    });
  });
}
